package com.league.zjxn.main.home

import android.text.Editable
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.BUNDLE_KEY_SEARCH
import com.league.zjxn.jetpack.vmodel.SearchViewModel
import com.league.zjxn.main.access.RegisterFragment
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_home_search.*
import java.lang.Exception

class HomeSearchActivity : OwnerViewModeActivity<SearchViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(HomeSearchActivity::class.java)
        }
    }

    //搜索记录碎片
    private val mSearchRecordFragment: SearchRecordFragment by lazy {
        SearchRecordFragment.newInstance()
    }

    //搜索游戏碎片
    private val mSearchResultFragment: SearchResultFragment by lazy {
        SearchResultFragment.newInstance()
    }

    override fun viewModel(): SearchViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_home_search

    override fun stateBar(): StatusBar {
        return StatusBar(view = llHomeSearchBar)
    }

    override fun initView() {

        ivHomeSearchBack.setOnClickListener { onBackPressed() }

        showRecordFragment()

        tvHomeSearchSubmit.setOnClickListener {
            val content = etHomeSearchContent.text.toString()
            if (content.isEmpty())
                return@setOnClickListener
            viewModel.keywordLiveData.postValue(content)
        }
    }

    override fun initData() {
        viewModel.searchRecord()
    }

    override fun initViewMode() {
        viewModel.keywordLiveData.observe(this as LifecycleOwner, { keyword ->
            etHomeSearchContent.text =
                Editable.Factory.getInstance().newEditable(keyword)

            val searchList = viewModel.mSearchRecordList.apply {
                remove(keyword)
                add(0, keyword)
            }
            SPStaticUtils.put(BUNDLE_KEY_SEARCH, GsonUtils.toJson(searchList))

            showGameFragment()

            viewModel.homeSearch(keyword)
        })
    }

    private fun showRecordFragment() {
        supportFragmentManager.beginTransaction().apply {
            if (!mSearchRecordFragment.isAdded) {
                add(R.id.flHomeSearchBody, mSearchRecordFragment)
            } else if (mSearchRecordFragment.isHidden) {
                show(mSearchRecordFragment)
            }
            if (mSearchResultFragment.isAdded)
                hide(mSearchResultFragment)
        }.commit()
    }

    private fun showGameFragment() {
        supportFragmentManager.beginTransaction().apply {
            if (!mSearchResultFragment.isAdded) {
                add(R.id.flHomeSearchBody, mSearchResultFragment)
                addToBackStack(null)
            } else if (mSearchResultFragment.isHidden) {
                show(mSearchResultFragment)
            }
            if (mSearchRecordFragment.isAdded)
                hide(mSearchRecordFragment)
        }.commit()
    }
}